package cn.yedong.org.service.impl;


import cn.yedong.basic.service.Impl.BaseServiceImpl;
import cn.yedong.org.domain.Department;
import cn.yedong.org.mapper.DepartmentMapper;
import cn.yedong.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
//
//    @Transactional
//    @Override
//    public void save(Department department) {
//        departmentMapper.add(department);
//    }
//
//    @Transactional
//    @Override
//    public void update(Department department) {
//        departmentMapper.update(department);
//    }
//
//    @Transactional
//    @Override
//    public void del(Long id) {
//        departmentMapper.del(id);
//    }
//
//    @Override
//    public Department loadById(Long id) {
//        return departmentMapper.loadById(id);
//    }
//
//    @Override
//    public List<Department> loadAll() {
//        return departmentMapper.loadAll();
//    }
//
//    @Override
//    public PageList<Department> queryDept(DepartmentQuery departmentQuery) {
//        //查询数据总量，分页栏使用
//        Integer current = departmentMapper.queryCount(departmentQuery);
//
//        //如果数据总量为0，则不进行当前页数据查询
//        if (current == 0) {
//            return new PageList<Department>();
//        }
//        //查询当前页数据
//        List<Department> datas = departmentMapper.queryData(departmentQuery);
//
//        //将总数量和当前页数据封装成集合返回
//        return new PageList<Department>(current, datas);
//    }
//
//    @Override
//    public void patchDel(List<Long> ids) {
//        departmentMapper.patchDel(ids);
//    }

    //获取部门树
    @Override
    public List<Department> deprTree() {
        //创建一个Map集合，将所有部门信息放在Map集合中，方便根据id获取部门信息，就不需要再循环数据库
        //1.1创建Map集合
        HashMap<Long, Department> map = new HashMap<>();
        //1.2查询所有部门
        List<Department> departments = departmentMapper.loadAll();
        //1.3遍历部门集合装进Map
        for (Department department : departments) {
            map.put(department.getId(), department);
        }
        //2.遍历所有部门，如果是顶级部门【parent_id == null】，就将它装到部门树
        //2.1创建List集合，用于装 无限极部门树
        ArrayList<Department> deptTree = new ArrayList<>();
        //2.2遍历部门集合
        for (Department department : departments) {
            //如果这个部门没有父id就将它直接装进部门树
            if (department.getParent_id() == null) {
                deptTree.add(department);
                //3.如果不是顶级部门，找到自己的父部门，将自己加到父部门的children
            } else {
                //3.1获取父部门id
                Long parentId = department.getParent_id();
                //3.2根据父部门id查询父部门
                Department parentDepartment = map.get(parentId);
                //防止父部门为空时报空指针异常
                if (parentDepartment != null) {
                    //3.3将自己装到父部门children
                    parentDepartment.getChildren().add(department);
                }
            }
        }
        return deptTree;
    }
}
